/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treepro;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

/**
 *
 * @author Ivan
 */
public abstract class GraphicObject implements Comparable<Object> {
    
    private double left = 0;
    private double top = 0;
    private double right = 0;
    private double bottom = 0;
    private TreePanel parent = null;
    private String name = "";
    private Color color = Color.BLACK;
    private Color selectedColor = Color.BLUE;
    private boolean selected = false;
    private Color solutionColor = Color.RED;
    private boolean solution = false;

    public boolean isSolution() {
        return solution;
    }

    public void setSolution(boolean solution) {
        this.solution = solution;
        repaint();
    }

    public Color getSolutionColor() {
        return solutionColor;
    }

    public void setSolutionColor(Color solutionColor) {
        this.solutionColor = solutionColor;
        repaint();
    }
    
    
    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
        //if (selected)
        repaint();
    }

    public Color getSelectedColor() {
        return selectedColor;
    }

    public void setSelectedColor(Color selectedColor) {
        this.selectedColor = selectedColor;
        repaint();
    }

    public float getCenterX() {
        return (float)(getLeft()+getWidth()/2);
    }
    public float getCenterY() {
        return (float)(getTop()+getHeight()/2);
    }
    
    public void checkPosAndSize() {
        if (parent!=null) {
            if (left<0) {
                right -= left;
                left = 0;
            }
            if (top+getHeight()>parent.getScreenHeight()) {
                top = parent.getScreenHeight()-getHeight();
                bottom = parent.getScreenHeight();
            }
                
            if (left+getWidth()>parent.getScreenWidth()) {
                left = parent.getScreenWidth()-getWidth();
                right = parent.getScreenWidth();
            }
            
            if (top<0) {
                bottom-=top;
                top = 0;
            }
                
        }
        /*double t = 0;
        if (right<left) {
            t = right;
            right = left;
            left = t;
            rev = !rev;
        }
        if (bottom<top) {
            t = bottom;
            bottom = top;
            top = t;
        }*/
        repaint();
    }
    
    public void setRect(double left, double top, double right, double bottom) {
        this.left = left;
        this.right = right;
        this.top = top;
        this.bottom = bottom;
        checkPosAndSize();
    }
    
    public void repaint() {
        if (parent!=null)
            parent.repaint();        
    }
    
    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
        repaint();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        repaint();
    }

            
    public double getHeight() {
        return bottom-top;
    }

    public void setHeight(double height) {
        setBottom(top+height);
    }

    public double getWidth() {
        return right-left;
    }

    public void setWidth(double width) {
        setRight(left+width);
    }
    

    public TreePanel getParent() {
        return parent;
    }

    public void setParent(TreePanel parent) {
        this.parent = parent;
        repaint();
    }
    
    public double getBottom() {
        return bottom;
    }

    public void setBottom(double bottom) {
        this.bottom = bottom;
        checkPosAndSize();        
    }

    public double getLeft() {
        return left;
    }

    public void setLeft(double left) {
        this.left = left;
        checkPosAndSize();
    }

    public double getRight() {
        return right;
    }

    public void setRight(double right) {
        this.right = right;
        checkPosAndSize();
    }

    public double getTop() {
        return top;
    }

    public void setTop(double top) {
        this.top = top;
        checkPosAndSize();
    }

    /**
     * Возвращает правду если х и у внутри прямоугольной зоны объекта
     * @param x
     * @param y
     * @return 
     */
    public boolean pointOnMe(double x, double y) {
        
        boolean l = false;
        boolean t = false;
        
        if (getLeft()<=getRight())
            l = (x>=getLeft())&&(x<=getRight());
        else
            l = (x<=getLeft())&&(x>=getRight());
        if (getTop()<=getBottom())
            t = (y>=getTop())&&(y<=getBottom());
        else
            t = (y<=getTop())&&(y>=getBottom());
        return l&&t;
    }
    
    public abstract void paint(Graphics2D g);
    public abstract void onMouseDragged(GraphicObject source, MouseEvent evt);
    public abstract void onMousePressed(GraphicObject source, MouseEvent evt);
    public abstract void onMouseMoved(GraphicObject source, MouseEvent evt);
    public abstract void onMouseReleased(GraphicObject source, MouseEvent evt);

    public int compareTo(Object o) {
        if (o instanceof NodeObject) return 0;
        return 1;
    }
    
    
}
